package com.qujia.station.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qujia.common.core.domain.UniversalRedisMessageDto;
import com.qujia.common.enums.WxConsumptionMessageEnum;
import com.qujia.common.exception.BaseException;
import com.qujia.customer.domain.CustomerConsumptionReport;
import com.qujia.station.domain.Enterprise;
import com.qujia.station.domain.PayOrder;
import com.qujia.station.mapper.AppUserInfoMapper;
import com.qujia.station.mapper.PayOrderAllMapper;
import com.qujia.station.service.IEnterpriseService;
import com.qujia.station.service.IPayOrderAllService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service("payOrderAllService")
@Slf4j
public class PayOrderAllServiceImpl implements IPayOrderAllService {

    @Value("${message.channel}")
    private String channel;

    @Autowired
    private PayOrderAllMapper payOrderAllMapper;
    @Autowired
    private IEnterpriseService iEnterpriseService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void addNewPayOrder() {
        PayOrder payOrder = payOrderAllMapper.selectLast();
//        if(Objects.isNull(payOrder)){
//            payOrderAllMapper.insetPayOrderHistory();
//            payOrderAllMapper.insetPayOrder();
//            return;
//        }
        if (Objects.isNull(payOrder)) {
            throw new BaseException("pay_order_all 数据表为空");
        }
        log.info("addNewPayOrder 同步订单前，最后一条订单：[{}]", JSONObject.toJSONString(payOrder));
        payOrderAllMapper.insetNewPayOrder(payOrder.getOrderId());
    }


    @Override
    public void addPayOrder(PayOrder payOrder) {
        if (Objects.isNull(payOrder)) {
            throw new BaseException("请求参数异常");
        }

        payOrderAllMapper.insertSelective(payOrder);
    }

    @Override
    public void updatePayOrder(PayOrder payOrder) {
        if (Objects.isNull(payOrder)) {
            throw new BaseException("请求参数异常");
        }
        payOrderAllMapper.updateByPrimaryKeySelective(payOrder);
    }

    @Override
    @Transactional
    public int deletePay(List<Long> list) {
        return payOrderAllMapper.deletePay(list);
    }


    @Override
    @Transactional
    public int deleteAllPay(PayOrder payOrder) {
        return payOrderAllMapper.deleteAllPay(payOrder);
    }

    @Override
    @Transactional
    public int deleteAllPay2(PayOrder payOrder) {
        return payOrderAllMapper.deleteAllPay2(payOrder);
    }

    @Override
    @Transactional
    public int deleteAllPay3(PayOrder payOrder) {
        return payOrderAllMapper.deleteAllPay3(payOrder);
    }

    @Override
    public void getTotalAmountByPayDate(LocalDateTime startTime, LocalDateTime endTime,List<String> appUserMobilePhone ) {
        List<PayOrder> payOrderInfoByPayDate = payOrderAllMapper.getPayOrderInfoByPayDate(startTime.toString(), endTime.toString(), appUserMobilePhone);
        for (PayOrder payOrder : payOrderInfoByPayDate) {

            CustomerConsumptionReport customerConsumptionReport = new CustomerConsumptionReport();
            customerConsumptionReport.setStationId(payOrder.getStationId());
            customerConsumptionReport.setStationName(payOrder.getStationName());
            customerConsumptionReport.setEnterpriseId(payOrder.getEnterpriseId());
            customerConsumptionReport.setAmtTotal(payOrder.getAmt());
            customerConsumptionReport.setOpenId(payOrder.getCustomerPhone());
            customerConsumptionReport.setDate(startTime.toLocalDate().toString().concat("~").concat(endTime.toLocalDate().toString()));

            UniversalRedisMessageDto dto = new UniversalRedisMessageDto();
            dto.setData(JSONObject.toJSONString(customerConsumptionReport));
            dto.setMessageType(WxConsumptionMessageEnum.MONTHLY_CONSUMPTION_REPORT_PUSH.getValue());
            dto.setTitle(WxConsumptionMessageEnum.MONTHLY_CONSUMPTION_REPORT_PUSH.getKey());

            redisTemplate.convertAndSend(channel, dto);

        }
    }

}
